/**
 * 
 */
package com.newtribe.wsdl;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.wsdl.Binding;
import javax.wsdl.BindingInput;
import javax.wsdl.BindingOperation;
import javax.wsdl.BindingOutput;
import javax.wsdl.Definition;
import javax.wsdl.Input;
import javax.wsdl.Message;
import javax.wsdl.Operation;
import javax.wsdl.OperationType;
import javax.wsdl.Output;
import javax.wsdl.Part;
import javax.wsdl.Port;
import javax.wsdl.PortType;
import javax.wsdl.Service;
import javax.wsdl.extensions.ExtensibilityElement;
import javax.wsdl.extensions.soap.SOAPAddress;
import javax.wsdl.extensions.soap.SOAPBinding;
import javax.wsdl.extensions.soap.SOAPBody;
import javax.wsdl.factory.WSDLFactory;
import javax.wsdl.xml.WSDLReader;
import javax.wsdl.xml.WSDLWriter;
import javax.xml.namespace.QName;

import com.ibm.wsdl.extensions.soap.SOAPAddressImpl;
import com.ibm.wsdl.extensions.soap.SOAPBindingImpl;
import com.ibm.wsdl.extensions.soap.SOAPBodyImpl;
/**
 * @author newtribe
 *
 */
public class WsdlUtils {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
		// TODO Auto-generated method stub
		//readWsdl();
		writeWsdl();
	}
	
	public void doStart() throws Exception  {
		readWsdl();
	}
	public static void readWsdl() throws Exception{
		WSDLFactory   factory   =   WSDLFactory   .newInstance(); 
		WSDLReader   reader   =   factory.newWSDLReader(); 
	
		reader.setFeature( "javax.wsdl.verbose",false); 
		reader.setFeature( "javax.wsdl.importDocuments",true);
		
		String   wsdlURL   =   "http://210.25.9.38:8888/axis/services/SecureService?wsdl"; 
		Definition   def   =   reader.readWSDL(null,wsdlURL); 

		//一个WSDL可以定义多个服务,我只取了第一个 
		Service   service   =   (Service)   getFirstItem(def.getServices());     //service就是第一个定义的服务的对象。 
		Map   ports   =   service.getPorts();       //ports:服务对应的接口,服务可能有多个接口，我只取其中的HTTP   SOAP接口。可能是HTTP   GET,HTTP   POST,HTTP   SOAP方式等等。即此服务支持的访问方式。 
		Iterator   iter   =   ports.values().iterator(); 
		String   url=null; 
		Port   port=null; 
		ExtensibilityElement   tmp=null; 
		while(iter.hasNext()) 
		{ 
		port   =   (Port)iter.next(); 
		tmp=   (ExtensibilityElement)port.getExtensibilityElements().get(0); 
	

		//多个接口的问题？ 
		Binding   binding   =   port.getBinding();   //binding：包含了接口访问的定义。 
		List   operationList   =   binding.getBindingOperations();     //operationList   可以理解为服务提供的方法的列表。 
		int   size   =   operationList.size(); 
		for   (int   i   =   0;   i   <   size;   i++)   { 
		
			BindingOperation   oper   =   (BindingOperation)   operationList.get(i); 
			System.out.println(oper.toString());
		}
		}
	}
	
	private  static  Object   getFirstItem(Map   map)   { 
		Iterator   keys   =   map.keySet().iterator(); 
		return   map.get(keys.next()); 
		} 
	
	public static void writeWsdl()throws Exception{
		
		String tns = "http://test.org/Hello";   
		String xsd = "http://www.w3.org/2001/XMLSchema";   

		WSDLFactory wsdlFactory = WSDLFactory.newInstance();   
		// 创建一个 WSDL definition   
		Definition definition = wsdlFactory.newDefinition();   
		// 设置 targetNamespace   
		definition.setTargetNamespace(tns);   
		definition.setQName(new QName(tns,"Hello"));   
		// 添加命名空间 (一些常用的命名空间)   
		definition.addNamespace("tns", tns);   
		definition.addNamespace("xsd", xsd);   
		definition.addNamespace("wsdlsoap", "http://schemas.xmlsoap.org/wsdl/soap/");   
		definition.addNamespace("soapenc11", "http://schemas.xmlsoap.org/soap/encoding/");   
		definition.addNamespace("soapenc12", "http://www.w3.org/2003/05/soap-encoding");   
		definition.addNamespace("soap11", "http://schemas.xmlsoap.org/soap/envelope/");   
		definition.addNamespace("soap12", "http://www.w3.org/2003/05/soap-envelope");   

		//创建 Part   
		Part part1 = definition.createPart();   
		part1.setName("part");   
		//设置 part1 的 Schema Type 为 xsd:string   
		part1.setTypeName(new QName(xsd,"string"));   
		Part part2 = definition.createPart();   
		part2.setName("part");   
		//设置 part2 的 Schema Type 为 xsd:string   
		part2.setTypeName(new QName(xsd,"string"));   

		//创建消息（Message）   
		Message message1 = definition.createMessage();   
		message1.setQName(new QName(tns,"helloRequest"));   
		//为 message 添加 Part   
		message1.addPart(part1);   
		message1.setUndefined(false);   
		Message message2 = definition.createMessage();   
		message2.setQName(new QName(tns,"helloResponse"));   
		message2.addPart(part2);   
		message2.setUndefined(false);   
		definition.addMessage(message1);   
		definition.addMessage(message2);   

		//创建 portType   
		PortType portType = definition.createPortType();   
		portType.setQName(new QName(tns,"helloPortType"));   
		//创建 Operation   
		Operation operation = definition.createOperation();   
		operation.setName("hello");   
		//创建 Input，并设置 Input 的 message   
		Input input = definition.createInput();   
		input.setName("helloRequest");   
		input.setMessage(message1);   
		//创建 Output，并设置 Output 的 message   
		Output output = definition.createOutput();   
		output.setName("helloResponse");   
		output.setMessage(message2);   
		//设置 Operation 的输入，输出，操作类型   
		operation.setInput(input);   
		operation.setOutput(output);   
		operation.setStyle(OperationType.REQUEST_RESPONSE);   
		//这行语句很重要 ！   
		operation.setUndefined(false);   
		portType.addOperation(operation);   
		portType.setUndefined(false);   
		definition.addPortType(portType);   

		//创建绑定（binding）   
		Binding binding = definition.createBinding();   
		binding.setQName(new QName(tns,"helloHttpBinding"));   
		//创建SOAP绑定（SOAP binding）   
		SOAPBinding soapBinding = new SOAPBindingImpl();   
		//设置 style = "document"   
		soapBinding.setStyle("document");   
		//设置 SOAP传输协议 为 HTTP   
		soapBinding.setTransportURI("http://schemas.xmlsoap.org/soap/http");   
		//soapBinding 是 WSDL 中的扩展元素，   
		//为 binding 添加扩展元素 soapBinding    
		binding.addExtensibilityElement(soapBinding);   
		//设置绑定的端口类型   
		binding.setPortType(portType);   
		//创建绑定操作（Binding Operation）   
		BindingOperation bindingOperation = definition.createBindingOperation();   
		//创建 bindingInput   
		BindingInput bindingInput = definition.createBindingInput();   
		bindingInput.setName("helloRequest");   
		//创建 SOAP body ，设置 use = "literal"   
		SOAPBody soapBody1 = new SOAPBodyImpl();   
		soapBody1.setUse("literal");   
		bindingInput.addExtensibilityElement(soapBody1);   
		BindingOutput bindingOutput = definition.createBindingOutput();   
		bindingOutput.setName("helloResponse");   
		SOAPBody soapBody2 = new SOAPBodyImpl();   
		soapBody2.setUse("literal");   
		bindingOutput.addExtensibilityElement(soapBody2);   
		//设置 bindingOperation 的名称，绑定输入 和 绑定输出   
		bindingOperation.setName("hello");     
		bindingOperation.setBindingInput(bindingInput);   
		bindingOperation.setBindingOutput(bindingOutput);      
		binding.addBindingOperation(bindingOperation);   
		//这行语句很重要 ！   
		binding.setUndefined(false);   
		definition.addBinding(binding);    

		//创建 service   
		Service service = definition.createService();   
		service.setQName(new QName(tns,"helloService"));   
		//创建服务端口 port   
		Port port = definition.createPort();   
		//设置服务端口的 binding，名称，并添加SOAP地址   
		port.setBinding(binding);   
		port.setName("helloHttpPort");   
		SOAPAddress soapAddress = new SOAPAddressImpl();   
		soapAddress.setLocationURI("http://test.org/services/hello");   
		port.addExtensibilityElement(soapAddress);   
		service.addPort(port);   
		definition.addService(service);   



		//打印刚创建的 WSDL   
		WSDLWriter writer = wsdlFactory.newWSDLWriter();   
		writer.writeWSDL(definition, System.out);


	}

}
